<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>功能列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <link rel="stylesheet" href="../css/zTreeStyle/zTreeStyle.css">
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script src="../js/base.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/jquery.ztree.core-3.5.js"></script>
    <script src="../js/jquery.ztree.excheck-3.5.js"></script>
    <script src="../js/jquery.form.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<style>
    .layui-btn {
        height: 30px !important;
        line-height: 30px !important;
    }
</style>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card" style="display: flex;justify-content: space-between;">
                <div class="layui-card-body ">
                    <blockquote class="layui-elem-quote" style="border:0;font-size: 18px;color: #000;">功能列表
                    </blockquote>
                </div>
            </div>
        </div>
        <div class="layui-col-md12">
            <div class="layui-col-md6">
                <div style="margin-left:10px;margin-top:10px;">
                    <div class="layui-form-item" style="text-align: left">
                        <button type="button" lay-filter="add"
                                class="layui-btn" onclick="resetFun()">添加
                        </button>
                    </div>
                </div>
                <div style="margin-left:100px;">
                    <ul id="tree" class="ztree"></ul>
                </div>
            </div>
            <div class="layui-col-md6">
                <form class="layui-form" name="funForm" id="funForm" style="width: 600px;margin: 40px auto;"
                      action=""
                      method="post">
                    <div class="layui-form-item">
                        <input type="hidden" id="parentId" name="fun.parentId"
                               value=""/>
                        <input type="hidden" id="isParent"/>
                        <input type="hidden" id="isSave" value="0"/>
                    </div>


                    <div class="layui-form-item">
                        <label for="funId" class="layui-form-label">
                            功能Id
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="funId" name="fun.funId" value="0" autocomplete="off"
                                   class="layui-input" readonly>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="funName" class="layui-form-label">
                            <span class="x-red">*</span>功能名称
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="funName" name="fun.funName" required=""
                                   lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="funName" class="layui-form-label">
                            <span class="x-red">*</span>跳转地址
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="href" name="fun.href" required=""
                                   lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="funName" class="layui-form-label">
                            <span class="x-red">*</span>高亮名称(开发用)
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="active" name="fun.active" required=""
                                   lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="funName" class="layui-form-label">
                            <span class="x-red">*</span>上一节点
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" id="parentName" name="fun.funName" required=""
                                   lay-verify="required" autocomplete="off" class="layui-input" readonly>
                        </div>
                    </div>
                </form>
                <div class="layui-form-item" style="text-align: center">
                    <button type="button" lay-filter="add"
                            class="layui-btn" onclick="submitFun()">保存
                    </button>
                    <button type="button" lay-filter="add"
                            class="layui-btn" onclick="deleteFun()">删除
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'layer'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
        });

    function resetFun() {
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getSelectedNodes(true);
        if (nodes == null || nodes.length == 0) {
            toastr["warning"]("请先选择父节点", "提示");
            return;
        }
        document.funForm.reset();
        $("#parentName").val(nodes[0].funName);
        $("#parentId").val(nodes[0].funId);
        $("#funId").val('');
        $("#isSave").val(1);
    }

    function checkFun() {
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
            //alert(nodes[i].funid);
        }
    }


    //树配置
    var setting = {
        data: {
            key: {
                name: "funName"
            },
            simpleData: {
                enable: true,
                idKey: 'funId',
                pIdKey: 'parentId'
            }
        },
        callback: {
            onClick: onClick,
            onCheck: onCheck
        }
    };


    function onCheck(event, treeId, treeNode) {
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getCheckedNodes(true);
        /* for (var i = 0; i < nodes.length; i++) {
            alert(nodes[i].funid);
        } */
    }


    //选中树节点时的操作
    function onClick(event, treeId, treeNode) {
        var funid = treeNode.funId;
        $("#isSave").val(0);//修改操作
        if (funid != 0) {
            var params = {
                "funid": funid
            };
            if (treeNode.isParent) {
                $("#isParent").val(1);
            } else {
                $("#isParent").val(0);
            }
            $.post(getRequestPrefix() + "/admin/fun/funInfo", params, function (data) {
                if (data) {
                    $("#funId").val(data.funId);
                    $("#parentId").val(data.parentId);
                    $("#funName").val(data.funName);
                    $("#href").val(data.href);
                    $("#active").val(data.active);
                    $("#parentName").val(treeNode.getParentNode().funName);
                }
            });
        }
    };

    //绑定章节树数据
    function BindTree() {
        $.post(getRequestPrefix() + "/admin/fun/funList", function (data) {
            if (data.length != 0) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].funId == 0)
                        data[i].open = true;
                }
                $.fn.zTree.init($("#tree"), setting, data);
            }
        });
    };


    $(document).ready(function () {
        BindTree();
    });


    //删除当前栏目
    function deleteFun() {
        var funid = $("#funId").val();
        $.ajax({
            url: getRequestPrefix() + "/admin/fun/deleteFun/" + funid,
            dataType: "json",
            cache: false,
            error: function (textStatus, errorThrown) {
                layer.msg("系统ajax交互错误:" + textStatus)
            },
            success: function (data, textStatus) {
                // 如果读取结果成功，则将信息载入到对话框中
                var msg = data.msg;
                if (msg == "0") {
                    layer.msg("删除成功")
                    window.location.reload();
                } else {
                    layer.msg("error")
                }
            }
        });
    }


    function submitFun() {
        $("#funForm").attr("action", getRequestPrefix() + "/admin/fun/saveFun");
        var funName = $("#funName").val();
        // var funid = $("#funId").val();
        var isParent = $("#isParent").val();
        var href = $("#href").val();
        if (funName == '') {
            layer.msg("名称不能为空")
            return;
        }
        if ($("#isSave").val() != 1) {//保存
            if (href != 'javascript:;' && isParent == 1) {
                layer.msg("当前节点有子节点，不能修改跳转地址")
                return;
            }
        }

        $("#funForm").ajaxForm(function (data) {
            console.log(data)
            if (data.msg == '0') {
                layer.msg("保存成功");
                setTimeout(function () {
                    window.location.reload();
                }, 1000)
            }
        }).submit()
    }

</script>
</body>

</html>